package com.cy.store.service.impl;

import com.cy.store.entity.Product;
import com.cy.store.mapper.ProductMapper;
import com.cy.store.service.IProductService;
import com.cy.store.service.ex.ProductNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: AngJie
 * @create: 2022-02-09 15:27
 **/
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> findHotProduct() {
        List<Product> list = productMapper.findHotProduct();
        // 此处是单纯从数据库中查询数据，而不是验证是否数据存在，故查不到也没关系，说明无数据

        for (Product product :
                list) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return list;
    }

    @Override
    public Product findById(Integer id) {

        //根据参数id调用私有方法查询，获得商品数据
        Product product = productMapper.findById(id);
        //判断查询结果是否为null
        if (product == null) {
            throw new ProductNotFoundException("尝试查询的商品信息不存在");
        }
        //精简数据
        product.setPriority(null);
        product.setCreatedUser(null);
        product.setCreatedTime(null);
        product.setModifiedUser(null);
        product.setModifiedTime(null);

        //返回查询结果
        return product;

    }
}
